package cn.xiyue.boxuegu;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;

import cn.xiyue.boxuegu.adapter.NavAdapter;
import cn.xiyue.boxuegu.base.BaseActivity;
import cn.xiyue.boxuegu.ui.CourseFragment;
import cn.xiyue.boxuegu.ui.ExercisesFragment;
import cn.xiyue.boxuegu.ui.MyFragment;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";

    private TextView tv_back;
    private RelativeLayout rl_title_bar;
    private TextView tv_main_title;
    private ViewPager vp_fragment;
    private LinearLayout nav_course;
    private LinearLayout nav_exercises;
    private LinearLayout nav_my;
    private ImageView iv_course;
    private TextView tv_course;
    private ImageView iv_exercises;
    private TextView tv_exercises;
    private ImageView iv_my;
    private TextView tv_my;
    private long time = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
//        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
//        startActivityForResult(intent, 1);
    }

    private void initView() {
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_main_title = (TextView) findViewById(R.id.tv_main_title);
        vp_fragment = (ViewPager) findViewById(R.id.vp_fragment);
        nav_course = (LinearLayout) findViewById(R.id.nav_course);
        nav_exercises = (LinearLayout) findViewById(R.id.nav_exercises);
        nav_my = (LinearLayout) findViewById(R.id.nav_my);
        rl_title_bar = (RelativeLayout) findViewById(R.id.rl_title_bar);
        nav_course.setOnClickListener(this);
        nav_exercises.setOnClickListener(this);
        nav_my.setOnClickListener(this);
        tv_back.setVisibility(View.GONE);
        tv_main_title.setText("博学谷课程");
        rl_title_bar.setBackgroundColor(Color.parseColor("#666666"));
        iv_course = (ImageView) findViewById(R.id.iv_course);
        tv_course = (TextView) findViewById(R.id.tv_course);
        iv_exercises = (ImageView) findViewById(R.id.iv_exercises);
        tv_exercises = (TextView) findViewById(R.id.tv_exercises);
        iv_my = (ImageView) findViewById(R.id.iv_my);
        tv_my = (TextView) findViewById(R.id.tv_my);
        initViewPager();
        selectNav(0);
    }

    private void initViewPager() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new CourseFragment());
        fragments.add(new ExercisesFragment());
        fragments.add(new MyFragment());
        vp_fragment.setAdapter(new NavAdapter(getSupportFragmentManager(), fragments));
        vp_fragment.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                cleanStatus();
                selectNav(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @SuppressLint("ResourceAsColor")
    private void cleanStatus() {
        tv_course.setTextColor(R.color.nav_init);
        iv_course.setImageResource(R.drawable.main_course_icon);
        tv_exercises.setTextColor(R.color.nav_init);
        iv_exercises.setImageResource(R.drawable.main_exercises_icon);
        tv_my.setTextColor(R.color.nav_init);
        iv_my.setImageResource(R.drawable.main_my_icon);
        rl_title_bar.setVisibility(View.VISIBLE);
    }

    @SuppressLint("ResourceAsColor")
    private void selectNav(int position) {
        switch (position) {
            case 0:
                tv_course.setTextColor(R.color.nav_selected);
                iv_course.setImageResource(R.drawable.main_course_icon_selected);
                break;
            case 1:
                tv_exercises.setTextColor(R.color.nav_selected);
                iv_exercises.setImageResource(R.drawable.main_exercises_icon_selected);
                break;
            case 2:
                tv_my.setTextColor(R.color.nav_selected);
                iv_my.setImageResource(R.drawable.main_my_icon_selected);
                rl_title_bar.setVisibility(View.GONE);
                break;
        }
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nav_course:
                vp_fragment.setCurrentItem(0);
                break;
            case R.id.nav_exercises:
                vp_fragment.setCurrentItem(1);
                break;
            case R.id.nav_my:
                vp_fragment.setCurrentItem(2);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - time > 2000){
            showMsg("再按一次退出程序！");
            time = System.currentTimeMillis();
        }else {
            super.onBackPressed();
        }
    }
}